import streamlit as st
import re
import data.data as dd
import time
import base64
st.set_page_config(
    page_title="问答助手重置密码页面",
    page_icon="🤖"
)

st.html("""
    <style>
        #root > div:nth-child(1) > div.withScreencast > div > div > div > section > div.block-container.st-emotion-cache-13ln4jf.ea3mdgi5 > div > div > div > div.st-emotion-cache-ocqkz7.e1f1d6gn5 > div.st-emotion-cache-1dmwbxo.e1f1d6gn3 > div > div > div > div > div > button {
            border:0;
            background-color:transparent;
         }
         #root > div:nth-child(1) > div.withScreencast > div > div > div > section > div.block-container.st-emotion-cache-13ln4jf.ea3mdgi5 > div > div > div > div.st-emotion-cache-ocqkz7.e1f1d6gn5 > div.st-emotion-cache-1dmwbxo.e1f1d6gn3 > div > div > div > div > div > button > div > p {
         font-size: 0.8rem}
         #root > div:nth-child(1) > div.withScreencast > div > div > div > section > div.block-container.st-emotion-cache-13ln4jf.ea3mdgi5 > div > div > div > div.st-emotion-cache-ocqkz7.e1f1d6gn5 > div.st-emotion-cache-f7qlno.e1f1d6gn3 > div > div > div > div > div > button{
            border:0;
            background-color:transparent;
         }
         #root > div:nth-child(1) > div.withScreencast > div > div > div > section > div.block-container.st-emotion-cache-13ln4jf.ea3mdgi5 > div > div > div > div.st-emotion-cache-ocqkz7.e1f1d6gn5 > div.st-emotion-cache-f7qlno.e1f1d6gn3 > div > div > div > div > div > button > div > p{
         font-size: 0.8rem}
    </style>
"""
)

st.title('问答助手重置密码页面')
username = st.text_input("请输入账号")

password = st.text_input("请输入新密码",type='password')
repass = st.text_input("请再次输入你的新密码",type='password')
col1, col2 = st.columns([7,2.4])
with col1:
    resetFlag = st.button("确认重置")
with col2:
    loginFlag = st.button("取消重置，点击登录")


def reset(username,password,repass):
    if username and password and repass:
        if re.match('^(13|15|17|18|19)[0-9]{9}$', username):
                result = dd.query_user_password(username)
                if password == repass and len(password) >= 8:
                    if result["password"] != password:
                        if dd.query_user(username):
                            if dd.update_message(username,password):
                                st.success("重置成功")
                                time.sleep(1)
                                st.switch_page("login.py")
                            else:
                                    st.error('重置失败，请联系管理员')
                        else:
                                st.error("该用户不存在，请先注册")
                    else:
                        st.error("新密码不能和原密码一致")
                else:
                    st.error("两次密码必须一致或者密码长度不足8位")
        else:
            st.error("请输入正确的手机号")
    else:
        st.error("请完善信息")

if resetFlag:
    reset(username,password,repass)

if loginFlag:
    st.switch_page("login.py")

def main_bg(main_bg):
    main_bg_ext = "png"
    st.markdown(
        f"""
         <style>
         .stApp {{
             background: url(data:image/{main_bg_ext};base64,{base64.b64encode(open(main_bg, "rb").read()).decode()});
             background-size: cover
         }}
         </style>
         """,
        unsafe_allow_html=True
    )

# 调用
main_bg('images/login6.png')